<?php

/**
* AdminMenuTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminMenuTest extends WebTestCase
{

    private $testGuideName,$testSlideName,$testDesc,$testGuideId,$testSlideId,$url;
    private $username = '';
    private $password = '';


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $this->testGuideName = md5(uniqid(rand(), true));
        $this->testSlideName = md5(uniqid(rand(), true));
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->exec("INSERT INTO guide (name,owner) values ('".$this->testGuideName."','".$this->username."' )");
        $this->testGuideId =  $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide ( guide_id,name,owner) VALUES ('".$this->testGuideId."','".$this->testSlideName."','".$this->username."')");
        $this->testSlideId =  $dbh->lastInsertId();
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413"); 
        $this->get($this->url.'?cmd=AdminGuideList');
        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

    }

    public function testGuideList()
    {
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->assertText('Lista tras');
    }

    public function testGuideListRemoveGuide()
    {
        //>Lista tras>Usuń trasę
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->assertLinkById('AdminRemoveGuide'.$this->testGuideId);
        $this->clickLinkById('AdminRemoveGuide'.$this->testGuideId);
        $this->assertPattern("/.*$this->testGuideName.*/","URL: ".$this->url.'?cmd=AdminRemoveGuide&id='.$this->testGuideId." Does not remove guide ".$this->testGuideName);
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->clickSubmit("Tak");
        
    }


    public function testGuideListDoNotRemoveGuide()
    {
        // >Lista tras>Usuń trasę
        $this->get($this->url.'?cmd=AdminGuideList');

        $this->assertLinkById('AdminRemoveGuide'.$this->testGuideId);
        $this->clickLinkById('AdminRemoveGuide'.$this->testGuideId);
  
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->assertText('Usuń trasę');
        $this->clickSubmit('Nie');
    }


    public function testGuideListAddGuide()
    {
        //>Lista tras >Dodaj trasę 
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink('[Dodaj trasę]');
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->assertText('Dodaj trasę');
        $this->assertTrue($this->setFieldByName('name', $this->testGuideName )); 
        $this->click("Zapisz");
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->assertText('Dodaj trasę');

    }

    public function testGuideListShowRoute()
    {
        //>Lista tras >Pokaż trasę 
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->assertText('Pokaż trasę');
    }

    public function testGuideListShowRouteEditGuide()
    {

        //>Lista tras >Pokaż trasę >Edycja trasy
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->clickLink($this->testGuideName);
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Edycja trasy');
        $this->click("Zapisz");
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Edycja trasy');

    }


    public function testGuideListShowRouteAddSlide()
    {
        //>Lista tras >Pokaż trasę >Dodaj slajd 
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->clickLink('[Dodaj slajd]');
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Dodaj slajd');
        $this->assertTrue($this->setFieldByName('name', $this->testSlideName )); 
        $this->click("Zapisz");
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Dodaj slajd');

    }

    public function testGuideListShowRouteEditSlide()
    {
        //>Lista tras >Pokaż trasę >Edycja slajdu 
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->clickLink($this->testSlideName);
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Edycja slajdu');

        $this->click("Zapisz");

        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Edycja slajdu');

    }

    public function testGuideListShowRouteEditSlideRemoveSlide()
    {
        //>Lista tras >Pokaż trasę >Edycja slajdu > Usuń slajd
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->clickLink($this->testSlideName);
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Edycja slajdu');
        
        $this->clickSubmit("Usuń");

        $this->assertLink('Edycja slajdu', $this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId.'&id='.$this->testSlideId);
        $this->assertText('Usuń slajd');

        $this->clickSubmit("TAK");

        $this->assertText('Usuń slajd');
    }

    public function testGuideListShowRouteEditSlideDoNotRemoveSlide()
    {
       
        //>Lista tras >Pokaż trasę >Edycja slajdu > Usuń slajd
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->clickLink($this->testSlideName);
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Edycja slajdu');
        
        $this->click("Usuń");


        $this->assertLink('Edycja slajdu', $this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId.'&id='.$this->testSlideId);
        $this->assertText('Usuń slajd');

        $this->click("NIE");

        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);

        $this->assertText('Edycja slajdu');
        
        
    }

    public function testAuth()
    {
        //Login
        $this->get($this->url.'?cmd=AuthForm');
        $this->assertText('Login');
        $this->assertLink('Nowy użytkownik',$this->url.'?cmd=AuthRegister');
        //Logut form
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->assertLink('Logout',$this->url.'?cmd=AdminGuideList&logout=1');
        $this->assertText('Lista tras');
        //Register
        $this->get($this->url.'?cmd=AuthRegister');
        $this->assertLink('Login',$this->url.'?cmd=AuthForm');
        $this->assertText('Dodaj użytkownika');
    }


    public function testGuideListShowRouteSendCode()
    {
        //>Lista tras >Pokaż trasę 
        $this->get($this->url.'?cmd=AdminGuideList');
        $this->clickLink($this->testGuideName);
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->assertText('Pokaż trasę');
        $this->clickLink('[Wyślij kody]');
        $this->assertLink('Lista tras',$this->url.'?cmd=AdminGuideList');
        $this->assertLink('Pokaż trasę',$this->url.'?cmd=AdminShowRoute&id='.$this->testGuideId);
        $this->assertText('Wyślij kody');

    }

    public function tearDown() 
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("DELETE FROM slide WHERE guide_id=".$this->testGuideId);
        $dbh->exec("DELETE FROM guide WHERE rowid=".$this->testGuideId);
        $dbh->exec("DELETE FROM slide WHERE name='".$this->testSlideName."'");
        $dbh->exec("DELETE FROM guide WHERE name='".$this->testGuideName."'");
        $dbh = null;

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
        
    }
 

}
?>
